package com.tgy.crud.interceptor;

import com.tgy.crud.domain.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.interceptor.KeyGenerator;

import java.lang.reflect.Method;

@Slf4j
public class PersonKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object target, Method method, Object... params) {

        String methodName = method.getName();
        if (methodName.contains("find") || methodName.contains("delete")){

            Object param = params[0];

            return "person:" + param;
        }else if (methodName.contains("update")){

            Person person = (Person)params[0];

            return "person:" + person.getId();
        }

        return null;
    }
}
